package com.yunhe.option.service.base.impl;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.util.QueryUtil;
import com.yunhe.common.util.StringUtil;
import com.yunhe.option.domain.base.EstablishSoundManagementLedger;
import com.yunhe.option.repository.base.EstablishSoundManagementLedgerRepository;
import com.yunhe.option.service.base.EstablishSoundManagementLedgerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @ClassName EstablishSoundManagementLedgerServiceImpl
 * @Description 管理台账建立健全类型服务
 * @Author HeTao
 * @Date 2020/11/3 13:51
 * @Version 1.0
 **/
@Service
public class EstablishSoundManagementLedgerServiceImpl implements EstablishSoundManagementLedgerService {

    @Autowired
    private EstablishSoundManagementLedgerRepository establishSoundManagementLedgerRepository;

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public EstablishSoundManagementLedger createEstablishSoundManagementLedger(EstablishSoundManagementLedger establishSoundManagementLedger)
            throws UniqueConstraintsException, ArgumentErrorException {
        establishSoundManagementLedger.setId(null);
        processBeforeSave(establishSoundManagementLedger);
        return establishSoundManagementLedgerRepository.save(establishSoundManagementLedger);
    }

    /**
     * 管理台账建立健全类型入库前校验
     * @param establishSoundManagementLedger
     * @throws UniqueConstraintsException
     * @throws ArgumentErrorException
     */
    private void processBeforeSave(EstablishSoundManagementLedger establishSoundManagementLedger) throws UniqueConstraintsException, ArgumentErrorException {
        //判断管理台账建立健全类型代码、编号、标题是否为空
        if (establishSoundManagementLedger.getCode() == null
                || StringUtil.isEmpty(establishSoundManagementLedger.getName())
                || StringUtil.isEmpty(establishSoundManagementLedger.getTitle())) {
            throw new ArgumentErrorException("管理台账建立健全类型代码、编号、标题不能为空");
        }
        //判断代码是否已存在
        EstablishSoundManagementLedger typeByCode = this.findByCode(establishSoundManagementLedger.getCode());
        if (typeByCode != null) {
            if (establishSoundManagementLedger.getId() == null || typeByCode.getId().longValue() != establishSoundManagementLedger.getId().longValue()) {
                throw new UniqueConstraintsException("该管理台账建立健全类型代码已存在");
            }
        }
        //判断编号是否已存在
        EstablishSoundManagementLedger typeByName = this.findByName(establishSoundManagementLedger.getName());
        if (typeByName != null) {
            if (establishSoundManagementLedger.getId() == null || typeByName.getId().longValue() != establishSoundManagementLedger.getId().longValue()) {
                throw new UniqueConstraintsException("该管理台账建立健全类型编号已存在");
            }
        }
        //如果序号为空，则默认为代码
        if (establishSoundManagementLedger.getSn() == null && establishSoundManagementLedger.getCode() != null) {
            establishSoundManagementLedger.setSn(establishSoundManagementLedger.getCode());
        }
    }

    private EstablishSoundManagementLedger findByName(String name) {
        return establishSoundManagementLedgerRepository.findByName(name);
    }

    private EstablishSoundManagementLedger findByCode(Long code) {
        return establishSoundManagementLedgerRepository.findByCode(code);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public void deleteById(Long id) {
        establishSoundManagementLedgerRepository.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public EstablishSoundManagementLedger updateEstablishSoundManagementLedger(EstablishSoundManagementLedger establishSoundManagementLedger) throws ObjectNotFoundException, UniqueConstraintsException, ArgumentErrorException {
        boolean exists = establishSoundManagementLedger.getId() != null && establishSoundManagementLedgerRepository.existsById(establishSoundManagementLedger.getId());
        if (!exists) {
            throw new ObjectNotFoundException("找不到ID为" + establishSoundManagementLedger.getId() + "的管理台账建立健全类型");
        }
        processBeforeSave(establishSoundManagementLedger);
        return establishSoundManagementLedgerRepository.save(establishSoundManagementLedger);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public Iterable<EstablishSoundManagementLedger> findEstablishSoundManagementLedgers() {
        return establishSoundManagementLedgerRepository.findAll(QueryUtil.defaultSort());
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager")
    public EstablishSoundManagementLedger updateEstablishSoundManagementLedger(Long id, Long code, String name, String title, Long sn, Boolean activity)
            throws ObjectNotFoundException, UniqueConstraintsException, ArgumentErrorException {
        EstablishSoundManagementLedger establishSoundManagementLedger = this.findByEstablishSoundManagementLedgerId(id);
        if (establishSoundManagementLedger == null) {
            throw new ObjectNotFoundException("找不到ID为" + id + "的管理台账建立健全类型");
        }
        Optional.ofNullable(code).ifPresent(establishSoundManagementLedger::setCode);
        Optional.ofNullable(name).ifPresent(establishSoundManagementLedger::setName);
        Optional.ofNullable(title).ifPresent(establishSoundManagementLedger::setTitle);
        Optional.ofNullable(sn).ifPresent(establishSoundManagementLedger::setSn);
        Optional.ofNullable(activity).ifPresent(establishSoundManagementLedger::setActivity);
        processBeforeSave(establishSoundManagementLedger);
        return establishSoundManagementLedgerRepository.save(establishSoundManagementLedger);
    }

    private EstablishSoundManagementLedger findByEstablishSoundManagementLedgerId(Long id) {
        Optional<EstablishSoundManagementLedger> establishSoundManagementLedgerOptional = establishSoundManagementLedgerRepository.findById(id);
        EstablishSoundManagementLedger establishSoundManagementLedger = null;
        if(establishSoundManagementLedgerOptional.isPresent()){
            establishSoundManagementLedger = establishSoundManagementLedgerOptional.get();
        }
        return establishSoundManagementLedger;
    }
}
